package com.aifast.core.db.config;

import cn.hutool.core.lang.Console;
import com.aifast.common.cache.AutoCacheInnerInterceptor;
import com.aifast.common.forUpdate.ForUpdateInnerInterceptor;
import com.aifast.core.dataScope.DataScopeHandler;
import com.aifast.core.dataScope.DataScopeInnerInterceptor;
import com.aifast.core.db.handler.AiTenantHandler;
import com.aifast.common.properties.Setting;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@MapperScan("com.aifast.*.*.dao")
public class DbConfig {

    @Autowired
    private Setting setting;

    @Value("${ai.tenant.enable}")
    private boolean tenantEnable;

    @Value("${ai.data-scope.enable}")
    private boolean dataScopeEnable;

//    @Bean
    public HikariDataSource init(){
        HikariDataSource ds = new HikariDataSource();
        Console.log("******数据库初始化******" );
//        Console.log(setting);
        setting.init(ds);
        return ds;
    }

    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
//        paginationInnerInterceptor.setMaxLimit(1000l);
        paginationInnerInterceptor.setOptimizeJoin(true);

        /*启动数据权限插件*/
        if(dataScopeEnable){
            log.info("***启动数据权限插件***************************************************");
            DataScopeInnerInterceptor dataScope = new DataScopeInnerInterceptor();
            dataScope.setDataPermissionHandler(new DataScopeHandler());
            interceptor.addInnerInterceptor(dataScope);
        }

        /*启动多租户插件*/
        if(tenantEnable){
            TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
            tenantLineInnerInterceptor.setTenantLineHandler(new AiTenantHandler());
            interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
        }

        /*启动ForUpdate修改记录插件*/
        interceptor.addInnerInterceptor(new ForUpdateInnerInterceptor());

//        interceptor.addInnerInterceptor(new JoinInnerInterceptor());
        if(setting.getAutoCache())interceptor.addInnerInterceptor(new AutoCacheInnerInterceptor());


        /*启动mp分页插件*/
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        /*开启防止全表更新or删除插件*/
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }

    @Bean
    public MybatisConfiguration config(){
        MybatisConfiguration mb = new MybatisConfiguration();
        mb.setJdbcTypeForNull(null);
        mb.setCacheEnabled(true);
        mb.setMapUnderscoreToCamelCase(true);
        mb.setCallSettersOnNulls(true);
        mb.setUseActualParamName(true);

        GlobalConfig gc = new GlobalConfig();
        gc.setBanner(false);
        GlobalConfig.DbConfig dc = new GlobalConfig.DbConfig();
        dc.setIdType(IdType.AUTO);
        dc.setTableUnderline(true);
        dc.setLogicDeleteField("deleted");
        gc.setDbConfig(dc);

        return mb;
    }


}
